package com.crcode.crm.controller.accessory;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.crcode.crm.bean.accessory.AccessoryBean;
import com.crcode.crm.bean.log.DataLogBean;
import com.crcode.crm.bean.user.UserBean;
import com.crcode.crm.service.accessory.AccessoryService;
import com.crcode.crm.service.log.DataLogService;
import com.crcode.crm.utils.DateUtil;
import com.crcode.crm.utils.MyFileUtil;
import com.crcode.crm.utils.PageVo;
import com.crcode.crm.utils.ResultUtil;


/**
 * 附件控制层
 * @author cl
 * @date 2019年3月26日
 */
@Controller
public class AccessoryController {
	
	@Resource(name = "accessoryService")
	private AccessoryService accessoryService;
	@Resource(name = "logService")
	private DataLogService logService;
	
	/**
	 * 保存多个附件
	 * @param session
	 * @param accessorys
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value="/accessory/saveAccessorys",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saveAccessorys(HttpSession session,@RequestBody List<AccessoryBean> accessorys) throws ParseException{
		Map<String,Object> map=new HashMap<String, Object>();
		Object obj = session.getAttribute("user");
		UserBean user = JSONObject.parseObject(obj.toString(), UserBean.class);
		if(user==null){
			map.put("status", false);
			map.put("info", "未登录,请重新登录！");
			return map;
		}
		int count=0;
		String now=DateUtil.DateToStr(new Date(), "yyyy-MM-dd HH:mm:ss");
		String info="";
		for(AccessoryBean accessory:accessorys){
			AccessoryBean p=accessoryService.findByAccTitleAndDataUuid(accessory.getAccTitle(),accessory.getDataUuid());
			if(p!=null){
				//删除文件
				MyFileUtil.deleteFile(accessory.getAccFilePath());
				info+=accessory.getAccTitle()+",该名称已存在！";
			}else{
				accessory.setCreateUserUuid(user.getUuid());
				accessory.setCreateUserName(user.getUserRealName());
				accessory.setCreateTime(now);
				AccessoryBean acc=accessoryService.saveResultBean(accessory);
				if(acc!=null){
					count++;
				}else{
					//删除文件
					MyFileUtil.deleteFile(accessory.getAccFilePath());
				}
				
				//保存日志
				DataLogBean l = new DataLogBean();
				l.setUserUuid(user.getUuid());
				l.setUserName(user.getUserRealName());
				l.setOperateTime(now);
				l.setDataTuid(accessory.getDataUuid());
				l.setDataTitle(accessory.getDataName());
				l.setLatestContent(JSONObject.toJSONString(acc));
				l.setOriginalContent(null);
				l.setFileUuid(accessory.getUuid());
				l.setFileTitle(accessory.getAccTitle());
				l.setFileDescribe(accessory.getPlanDescribe());
				if(accessory.getAccType()==1){
					l.setOperateRemarks("企业附件新增");
					l.setLogType(0);
					l.setDetailedType(2);
				}else if(accessory.getAccType()==2){
					l.setOperateRemarks("项目方案新增");
					l.setLogType(1);
					l.setDetailedType(5);
				}
				logService.saveResultBoolean(l);
			}
			
			
		}
		map.put("status", count>0?true:false);
		map.put("count", count);
		map.put("info", info);
		return map;
	}
	
	/**
	 * 根据id查询
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/accessory/findById",method=RequestMethod.POST)
	@ResponseBody
	public AccessoryBean findById(HttpServletRequest request,Integer id){
		return accessoryService.getById(id);
	}
	
	/**
	 * 分页条件查询
	 * @param request
	 * @param conditions
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/accessory/findBySpec",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findBySpec(HttpServletRequest request,String conditions,PageVo vo){
		Map<String,Object> params=JSONObject.parseObject(conditions);
		return accessoryService.findAll(params,vo);
	}
	
	/**
	 * 根据主键批量物理删除
	 * @param request
	 * @param session
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/accessory/physicalDeleteByIds",method=RequestMethod.POST)
	@ResponseBody
	public ResultUtil physicalDeleteByIds(HttpServletRequest request,HttpSession session,String ids){
		return accessoryService.physicalDeleteByIds(ids);
	}
	
}
